package de.g18.it0a.jlaser.gui.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentListener;

import de.g18.it0a.jlaser.dataio.TestDataProvider;
import de.g18.it0a.jlaser.domain.Room;


public class MainView extends JFrame {

    private static final long serialVersionUID = 1L;

    private RoomEditorPanel roomEditorPanel;

    private JComboBox fileList;
    private JTextArea jlfEditor;
    private JButton solveButton;
    private JButton jlfSaveRoom;
    private JCheckBox jlfDebug;


    public MainView() {
        super("JLaser");
        setMinimumSize(new Dimension(800, 700));
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        initComponents();

        add(buildPanel());
        pack();
        setVisible(true);
    }

    private void initComponents() {
        roomEditorPanel = new RoomEditorPanel();
        List<String> availableFiles = new ArrayList<String>();
        availableFiles.add("");
        for (File file : TestDataProvider.getInstance().getConfigFiles()) {
            availableFiles.add(file.getAbsolutePath());
        }
        availableFiles.add("Durchsuchen...");
        fileList = new JComboBox(availableFiles.toArray(new String[availableFiles.size()]));
        solveButton = new JButton("Spiegelpositionen berechnen");
        jlfEditor = new JTextArea(10, 20);
        jlfSaveRoom = new JButton("Raum speichern");
        jlfSaveRoom.setEnabled(false);

        jlfDebug = new JCheckBox("SolutionManager debuggen");
        jlfDebug.setSelected(true);
    }

    private JComponent buildPanel() {
        JPanel testDataPanel = new JPanel(new BorderLayout());
        testDataPanel.setBorder(BorderFactory.createTitledBorder("Raum laden"));
        testDataPanel.add(fileList, BorderLayout.CENTER);

        JPanel solutionPanel = new JPanel(new BorderLayout());
        solutionPanel.setBorder(BorderFactory.createTitledBorder("Aktionen"));
        solutionPanel.add(solveButton, BorderLayout.CENTER);
        solutionPanel.add(jlfDebug, BorderLayout.EAST);

        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(testDataPanel, BorderLayout.NORTH);
        northPanel.add(solutionPanel, BorderLayout.SOUTH);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.add(northPanel, BorderLayout.NORTH);

        JPanel fileEditorPanel = new JPanel(new BorderLayout());
        fileEditorPanel.setBorder(BorderFactory.createTitledBorder("JLaserFormat - Editor"));
        fileEditorPanel.add(jlfEditor, BorderLayout.CENTER);
        fileEditorPanel.add(jlfSaveRoom, BorderLayout.SOUTH);
        mainPanel.add(fileEditorPanel, BorderLayout.EAST);

        JPanel editorPanel = new JPanel(new BorderLayout());
        editorPanel.setBorder(BorderFactory.createTitledBorder("Raum"));
        editorPanel.add(new JScrollPane(roomEditorPanel), BorderLayout.CENTER);
        mainPanel.add(editorPanel, BorderLayout.CENTER);

        return mainPanel;
    }

    public Room getRoom() {
        return roomEditorPanel.getRoom();
    }

    public void setRoom(Room aRoom) {
        roomEditorPanel.setRoom(aRoom);
    }

    public String getSelectedFile() {
        return (String) fileList.getSelectedItem();
    }

    public void setSelectedFile(String aFile) {
        fileList.getModel().setSelectedItem(aFile);
    }

    public void addItemToFileList(String aFile) {
        fileList.addItem(aFile);
    }

    public void addFileListSelectionChangeHandler(ActionListener aListener) {
        fileList.addActionListener(aListener);
    }

    public void addSolveButtonActionListener(ActionListener aListener) {
        solveButton.addActionListener(aListener);
    }

    public void setSolveButtonEnabled(boolean aEnabled) {
        solveButton.setEnabled(aEnabled);
    }

    public void addJLFEditorListener(DocumentListener aListener) {
        jlfEditor.getDocument().addDocumentListener(aListener);
    }

    public void setSaveActionEnabled(boolean aEnabled) {
        jlfSaveRoom.setEnabled(aEnabled);
    }

    public void addSaveActionListener(ActionListener aListener) {
        jlfSaveRoom.addActionListener(aListener);
    }

    public void setJLFEditorText(String aText) {
        jlfEditor.setText(aText);
    }

    public String getJLFEditorText() {
        return jlfEditor.getText();
    }

    public void setCalculating(boolean aCalculating) {
        roomEditorPanel.setCalculating(aCalculating);
    }

    public void addDebugListener(ActionListener aListener) {
        jlfDebug.addActionListener(aListener);
    }

    public boolean isDebugEnabled() {
        return jlfDebug.isSelected();
    }
}
